package com.zhupf.stack;

import java.util.Arrays;

/**
 * @author zhupf
 * @date 2024年12月06日 10:20
 * @Description
 */
public class SortedStack {

    public static void main(String[] args) {
        int[] nums = new int[3];
        nums = Arrays.copyOf(nums, 6);
        System.out.println(nums.length);

        SortedStack sortedStack = new SortedStack();
        sortedStack.push(1);
        sortedStack.push(2);
        System.out.println(sortedStack.peek());
        sortedStack.pop();
        System.out.println(sortedStack.peek());
    }

    private int[] stack;

    private int size;

    private int count;

    public SortedStack() {
        stack = new int[16];
        size = 16;
        count =0;
    }

    public void push(int val) {
        if(count == size){
            size = size << 1;
            stack = Arrays.copyOf(stack, size);
        }
        int idx = count-1;
        while (idx>=0 && stack[idx] <val){
            stack[idx+1] = stack[idx];
            idx--;
        }
        stack[idx+1] = val;
        count++;
    }

    public void pop() {
        if(count != 0){
            stack[count-1]=0;
            count--;
        }
    }

    public int peek() {
        if(count == 0){
            return -1;
        }
        return stack[count-1];
    }

    public boolean isEmpty() {
        return count == 0;
    }
}
